package com.sanley.piss.web.service;


import com.sanley.piss.mapper.DictionaryDataMapper;
import com.sanley.piss.mapper.DictionaryTypeMapper;
import com.sanley.piss.model.ServiceImpl;
import com.sanley.piss.model.StringUtil;
import com.sanley.piss.model.exceptionUtil.exception.ServiceException;
import com.sanley.piss.model.po.DictionaryData;
import com.sanley.piss.model.vo.DictionaryDataVO;
import com.sanley.piss.web.service.mapper.IDictionaryDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author 张凯大天才
 * @Date: 2021-3-16 11:37
 */
@Service
@Primary
public class DictionaryDataServiceImpl extends ServiceImpl<DictionaryDataMapper, DictionaryData> implements IDictionaryDataService {

    @Autowired
    private DictionaryTypeMapper dictionaryType;

    @Override
    public void insert(DictionaryDataVO dictionaryData) {
        dictionaryData.setState(0);
        dictionaryData.setDicTypeId(dictionaryType.selectIdByName(dictionaryData.getDicTypeName()));
        dictionaryData.setGmtCreate(LocalDateTime.now());
        baseMapper.insert(dictionaryData);

    }

    @Override
    public void removeByIds(Integer[] ids) {
        baseMapper.removeByIds(ids);
    }

    /**
     * 修改信息
     */
    @Override
    public void updateById(DictionaryData dictionaryData) {
        baseMapper.updateById(dictionaryData);
    }


    @Override
    public List<Map<Integer, Object>> selectDataIdAndName() {
        return baseMapper.selectDataIdAndName();
    }

    @Override
    public DictionaryData selectById(Integer id) {
        DictionaryData selectOne = baseMapper.selectById(id);
        if (StringUtil.isNull(selectOne)) {
            throw new ServiceException("信息不存在");
        }
        return selectOne;
    }
}
